package com.moneyhelper.upms.handler;

import com.moneyhelper.upms.constant.ContextConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * @author： ygl
 * @date： 2019/07/07
 * @Description：
 */
public class ContextHandler {

    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    public static void setUserId(String userId) {
        set(ContextConstant.CONTEXT_KEY_USER_ID, userId);
    }

    public static String getUserId() {
        return returnValue(get(ContextConstant.CONTEXT_KEY_USER_ID));
    }

    private static String returnValue(Object value) {
        return value == null ? null : value.toString();
    }
}
